package cn.haohaowo.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.http.Header;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration
public class RestTemplateConfig {

	@Bean("haoShopRestTemplate")
	public RestTemplate getRestTemplate(@Qualifier("haoShopClientHttpRequestFactory") ClientHttpRequestFactory requestFactory) {
		RestTemplate restTemplate = new RestTemplate();
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
		messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
		messageConverters.add(new FormHttpMessageConverter());
		messageConverters.add(new MappingJackson2HttpMessageConverter());
		messageConverters.add(new FastJsonHttpMessageConverter());
		restTemplate.setMessageConverters(messageConverters);
		restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
		restTemplate.setRequestFactory(requestFactory);
		
		return restTemplate;
	}
	
	@Bean(name = "haoShopClientHttpRequestFactory")
	public ClientHttpRequestFactory getClientHttpRequestFactory() {
		PoolingHttpClientConnectionManager poolingClientConnectionManager = new PoolingHttpClientConnectionManager(20, TimeUnit.SECONDS);
		poolingClientConnectionManager.setDefaultMaxPerRoute(1000);
		poolingClientConnectionManager.setMaxTotal(1000);
		
		HttpClientBuilder httpClientBuilder = HttpClients.custom();
		httpClientBuilder.setConnectionManager(poolingClientConnectionManager);
		List<Header> headers = new ArrayList<Header>();
		headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
		headers.add(new BasicHeader("Connection", "Keep-Alive"));
		httpClientBuilder.setDefaultHeaders(headers);
		httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
		
		HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
		clientHttpRequestFactory.setConnectionRequestTimeout(200);
		clientHttpRequestFactory.setReadTimeout(5000);
		clientHttpRequestFactory.setConnectTimeout(5000);
		clientHttpRequestFactory.setBufferRequestBody(true);
		
		return clientHttpRequestFactory;
	}
}
